class Solution {
public:
    int minNumberOfFrogs(string croakOfFrogs)
    {
        int hash[128] = { 0 };
        int prev[128] = { 0 };

        prev['c'] = 'k';
        prev['r'] = 'c';
        prev['o'] = 'r';
        prev['a'] = 'o';
        prev['k'] = 'a';

        int len = croakOfFrogs.size();
        for (int i = 0; i < len; i++)
        {
            int ch = croakOfFrogs[i];
            if (ch == 'c')
            {
                if (hash[prev[ch]])
                {
                    hash[prev[ch]]--;
                }
                hash[ch]++;
            }
            else
            {
                if (hash[prev[ch]])
                {
                    hash[prev[ch]]--;
                    hash[ch]++;
                }
                else
                {
                    return -1;
                }
            }
        }

        for (int i = 0; i < 128; i++)
        {
            if (i != 'k' && hash[i] != 0)
                return -1;
        }

        return hash['k'];
    }
};